﻿using System.Drawing;
using System.Windows.Forms;
using GuidanceExplorerAddIn.Properties;

namespace GuidanceExplorerAddIn.Controls
{
    public delegate void ClearNotesClickedHandler();
    //public class DataGridViewNotesHeaderCellEventArgs : EventArgs
    //{
    //    bool _bChecked;
    //    public DataGridViewNotesHeaderCellEventArgs(bool bChecked)
    //    {
    //        _bChecked = bChecked;
    //    }
    //    public bool Checked
    //    {
    //        get { return _bChecked; }
    //    }
    //}
    class DatagridViewNotesHeaderCell : DataGridViewColumnHeaderCell
    {
        Point buttonLocation;
        Size buttonSize;
        Point _cellLocation = new Point();

        public event ClearNotesClickedHandler OnClearNotesClicked;

        public DatagridViewNotesHeaderCell()
        {
        }

        protected override void Paint(System.Drawing.Graphics graphics,
            System.Drawing.Rectangle clipBounds,
            System.Drawing.Rectangle cellBounds,
            int rowIndex,
            DataGridViewElementStates dataGridViewElementState,
            object value,
            object formattedValue,
            string errorText,
            DataGridViewCellStyle cellStyle,
            DataGridViewAdvancedBorderStyle advancedBorderStyle,
            DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex,
            dataGridViewElementState, value,
            formattedValue, errorText, cellStyle,
            advancedBorderStyle, paintParts);
            
            Point p = new Point();
            Size s = new Size(cellBounds.Height - 6, cellBounds.Height - 6);
            p.X = cellBounds.Location.X + cellBounds.Width - s.Width * 2 - 8;// (cellBounds.Width / 2) - (s.Width / 2);
            p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2);
            _cellLocation = cellBounds.Location;
            buttonLocation = p;
            buttonSize = s;

            Rectangle buttonBounds = new Rectangle(p,s);
            Rectangle imageBounds = buttonBounds;
 
            Bitmap bitmap = Resources.Trash;
            bitmap.MakeTransparent();
            graphics.DrawImage(bitmap, imageBounds);
//            ButtonRenderer.DrawButton(graphics, buttonBounds, image, imageBounds, false, System.Windows.Forms.VisualStyles.PushButtonState.Default);
        }
        
        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
            if (p.X >= buttonLocation.X && p.X <= buttonLocation.X + buttonSize.Width
                  && p.Y >= buttonLocation.Y && p.Y <= buttonLocation.Y + buttonSize.Height)
            {                
                if (OnClearNotesClicked != null)
                {
                    OnClearNotesClicked();
                    this.DataGridView.InvalidateCell(this);
                }
            }
            base.OnMouseClick(e);
        }
    }
    /*
    This class needs to be included to the project to get the Header Checkbox feature.

    Then the checkbox need to be added to the datagridview as a DatagridViewCheckBoxColumn.
    Then we assign the header to it.
    The header is assigned an event with the help of which we can uncheck or check all the checkboxes.

    The code to add the checkbox to header and the corresponding event code is given below.
	
    private void FormatGrid()
    {
    DataView dv = new DataView();
    dv.Table = _loginDs.Tables[0];

    DataGridViewCheckBoxColumn chkbox = new DataGridViewCheckBoxColumn();
    DatagridViewCheckBoxHeaderCell chkHeader = new DatagridViewCheckBoxHeaderCell();
    chkbox.HeaderCell = chkHeader;
    chkHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(chkHeader_OnCheckBoxClicked);
    _chkBoxGrid.Columns.Add(chkbox);

    DataGridViewTextBoxColumn uname = new DataGridViewTextBoxColumn();
    uname.HeaderText = "user";
    uname.Name = "username";
    uname.DataPropertyName = "username";
    _chkBoxGrid.Columns.Add(uname);

    _chkBoxGrid.DataSource = dv;
    }

    void chkHeader_OnCheckBoxClicked(bool state)
    {
    foreach (DataGridViewRow row in _chkBoxGrid.Rows)
    row.Cells[0].Value = state;
    }*/

}
